#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGlobal>
#include <QTime>
#include <fstream>

enum {left, right};

Figure::Figure(QWidget* parent) : QLabel(parent)
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime())+Figure::seed);
    Figure::seed = qrand();
    id = qrand() % dataset_size;
    QString str_id = QString::number(id);
    QString filename_prefix = ":/";
    QString filename_suffix = ".png";

    for(int i=0; i<(4-str_id.length()); i++)
    {
        filename_prefix += "0";
    }
    QString filename = filename_prefix + str_id + filename_suffix;
    this->setPixmap(QPixmap(filename));
}

void Figure::refresh()
{
    std::ofstream fout("../compare.txt",std::ios::app);
    fout << id << " ";
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()) + Figure::seed);
    Figure::seed = qrand();
    id = qrand()%dataset_size;
    QString str_id = QString::number(id);
    QString filename_prefix = ":/";
    QString filename_suffix = ".png";

    for(int i=0; i<(4-str_id.length()); i++)
    {
        filename_prefix += "0";
    }
    QString filename = filename_prefix + str_id + filename_suffix;
    this->setPixmap(QPixmap(filename));
}

PushButton::PushButton(QWidget *parent, bool pos) : QPushButton(parent)
{
    position = pos;
}

void PushButton::record()
{
    std::ofstream fout("../compare.txt",std::ios::app);
    fout << position << std::endl;
    fout.close();
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Spacemash");
    this->setStyleSheet("background:white");
    this->setMinimumSize(1700,750);
    this->setMaximumSize(1700,750);

    title = new QLabel(this);
    title->setText("choose a picture with stronger sense of openness");
    title->setGeometry(680,0,600,50);

    //left image
    image1 = new Figure(this);
    image1->setGeometry(20,50,800,600);

    //right image
    image2 = new Figure(this);
    image2->setGeometry(870,50,800,600);

    //left button
    button1 = new PushButton(this,0);
    button1->setGeometry(300,670,150,50);
    button1->setText("left one");

    //right button
    button2 = new PushButton(this,1);
    button2->setGeometry(1200,670,150,50);
    button2->setText("right one");

    //connect button with label
    connect(button1,SIGNAL(clicked()),image1,SLOT(refresh()));
    connect(button1,SIGNAL(clicked()),image2,SLOT(refresh()));
    connect(button1,SIGNAL(clicked()),button1,SLOT(record()));

    connect(button2,SIGNAL(clicked()),image1,SLOT(refresh()));
    connect(button2,SIGNAL(clicked()),image2,SLOT(refresh()));
    connect(button2,SIGNAL(clicked()),button2,SLOT(record()));
}

MainWindow::~MainWindow()
{
    delete title;
    delete image1;
    delete image2;
    delete button1;
    delete button2;
    delete ui;
}

